<template>
  <div>
    <div
      style="padding-top: 30px"
      v-if="showSearch"
      :class="{
        'enter-level flipInX': showSearch,
      }"
    >
      <el-form :inline="true" :model="searchFrom" class="demo-form-inline">
        <el-row justify="space-around" :gutter="24">
          <el-col :span="5">
            <el-form-item label="订单号">
              <el-input
                v-model="searchFrom.orderId"
                placeholder="请输入订单号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="手机号">
              <el-input
                v-model.number="searchFrom.phone"
                placeholder="请输入手机号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态">
              <el-select
                v-model="searchFrom.status"
                placeholder="请选择"
                clearable
              >
                <el-option label="未支付" value="0" />
                <el-option label="已支付" value="1" />
                <el-option label="未完成" value="2" />
                <el-option label="已完成" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付类型">
              <el-select
                v-model="searchFrom.payType"
                placeholder="请选择"
                clearable
              >
                <el-option label="余额" value="balancepay" />
                <el-option label="微信" value="wechatpay" />
                <el-option label="支付宝" value="alipay" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row justify="space-around" :gutter="20">
          <el-col :span="5">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="searchFrom.createTime"
                type="date"
                placeholder="请选择"
                :size="size"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付时间">
              <el-date-picker
                v-model="searchFrom.payTime"
                type="date"
                placeholder="请选择"
                :size="size"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-button type="primary" @click="onSubmit" :icon="Search">
                搜索
              </el-button>
              <el-button type="info" @click="reset" :icon="Refresh">
                重置
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="5"> </el-col>
        </el-row>
      </el-form>
    </div>
    <div style="padding: 10px 30px">
      <div
        style="
          padding-bottom: 20px;
          display: flex;
          justify-content: space-between;
        "
      >
        <div>
          <!-- <el-button :icon="Plus" type="success" @click="insertOne">
            新增
          </el-button> -->
          <el-popconfirm
            title="确定删除吗？"
            @confirm="batchDel"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" :disabled="isBatch">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </div>
        <div>
          <el-button :icon="Search" circle @click="isSearch"></el-button>
          <el-button :icon="Refresh" circle @click="reset"></el-button>
        </div>
      </div>
      <el-table
        ref="multipleTableRef"
        :data="dataList"
        style="width: 100%"
        :height="tableHeight"
        @selection-change="handleSelectionChange"
        v-loading="loading"
      >
        <el-table-column type="selection" :selectable="selectable" width="55" />
        <el-table-column
          type="index"
          :index="indexMethod"
          label="编号"
          width="105"
          fixed
        />
        <el-table-column property="orderId" label="订单编号" width="200" />
        <el-table-column
          property="videoShop.title"
          label="商品名称"
          width="200"
        />
        <el-table-column label="下单时间" width="200">
          <template #default="scope">{{
            tool.formatDateToYYYYMMDD(scope.row.createTime)
          }}</template>
        </el-table-column>
        <el-table-column label="支付时间" width="200">
          <template #default="scope">{{
            tool.formatDateToYYYYMMDD(scope.row.payTime)
          }}</template>
        </el-table-column>
        <el-table-column
          property="videoUser.nickname"
          label="购买用户"
          width="200"
        />
        <el-table-column
          property="videoUser.phone"
          label="手机号"
          width="200"
        />
        <el-table-column property="price" label="总价" width="150" />
        <el-table-column property="payPrice" label="支付金额" width="150" />
        <el-table-column property="preferential" label="优惠金额" width="200" />
        <el-table-column label="状态" width="130">
          <template #default="scope">
            <el-tag :type="typeArr[scope.row.status].value" disable-transitions>
              {{ typeArr[scope.row.status].key }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" width="100">
          <template #default="scope">
            <el-popconfirm
              title="确定删除吗？"
              @confirm="oper(scope.row.orderId)"
              @cancel="cancelEvent"
            >
              <template #reference>
                <el-button type="text" :icon="Delete"> 删除 </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 10px">
        <el-pagination
          :hide-on-single-page="true"
          background
          layout="prev, pager, next"
          :total="page.total"
          :page-size="page.size"
          :page-count="page.pages"
          @current-change="changePage"
          :pager-count="15"
        />
      </div>

      <div class="aliform" v-html="aliform"></div>
    </div>
  </div>
</template>

<script setup>
import request from "@/assets/js/request";
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import tool from "@/assets/js/tool";
import { Search, Refresh, Delete } from "@element-plus/icons-vue";

const showSearch = ref(true);
const searchFrom = ref({
  orderId: "",
  pageNum: 1,
  pageSize: 20,
  payType: null,
  createTime: null,
  payTime: null,
  status: null,
  phone: "",
});

const aliform = ref("");
const typeArr = {
  0: {
    key: "未支付",
    value: "info",
  },
  1: {
    key: "已支付",
    value: "primary",
  },
  2: {
    key: "已取消",
    value: "warning",
  },
  3: {
    key: "已完成",
    value: "success",
  },
};
const loading = ref(false);
const tableHeight = ref(600);
const selectable = ref();
const dataList = ref([]);
const isBatch = ref(true);
const selectData = ref([]);
const page = ref({
  size: 20,
  num: 1,
  pages: 1,
  total: 0,
});

const indexMethod = (index) => {
  return index + 1;
};
//获取数据
const getDataList = () => {
  loading.value = true;
  searchFrom.value.pageNum = page.value.num;
  searchFrom.value.pageSize = page.value.size;
  request
    .post(`/order/list`, searchFrom.value)
    .then((res) => {
      if (res.code === 200) {
        dataList.value = res.data.records;
        page.value.num = res.data.current;
        page.value.pages = res.data.pages;
        page.value.total = res.data.total;
      } else {
        ElMessage.error(res.msg);
      }
      loading.value = false;
    })
    .catch((err) => {
      ElMessage.error(err);
    });

  setTimeout(() => {
    loading.value = false;
  }, 5000);
};

const isSearch = () => {
  showSearch.value = !showSearch.value;
  if (showSearch.value) {
    tableHeight.value = 600;
  } else {
    tableHeight.value = 700;
  }
};

const changePage = (pageIndex) => {
  console.log(pageIndex);
  page.value.num = pageIndex;
  getDataList();
};

const cancelEvent = () => {
  selectData.value = [];
};

const oper = (id) => {
  selectData.value = [id];
  batchDel();
};

// 批量删除
const batchDel = () => {
  request
    .post("/order/del", selectData.value)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("删除成功");
        getDataList();
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {
      console.log(err);
      ElMessage.error("网络错误");
    });
};

// const insertOne = () => {
//   request
//     .get("/pay/alipay?shopId=1")
//     .then((res) => {
//       if (res.code === 200) {
//         let divForm = document.getElementsByTagName("divform");
//         if (divForm.length) {
//           document.body.removeChild(divForm[0]);
//         }
//         const div = document.createElement("divform");
//         div.innerHTML = res.data;
//         document.body.appendChild(div);
//         document.forms[1].setAttribute("target", "_blank");
//         document.forms[1].submit();
//       }
//     })
//     .catch((err) => {
//       console.log(err);
//     });
// };

const onSubmit = () => {
  if (
    searchFrom.value.orderId == "" &&
    searchFrom.value.payType == null &&
    searchFrom.value.status == null &&
    searchFrom.value.phone === "" &&
    searchFrom.value.payTime === null &&
    searchFrom.value.createTime === null
  ) {
    return;
  }
  getDataList();
};
const reset = () => {
  searchFrom.value = {
    orderId: "",
    pageNum: 1,
    pageSize: 20,
    payType: null,
    createTime: null,
    payTime: null,
    status: null,
    phone: "",
  };
  getDataList();
};

// 多选
const handleSelectionChange = (val) => {
  let arr = val.map((item) => item.orderId);

  selectData.value = arr;
  if (selectData.value.length > 0) {
    isBatch.value = false;
  } else {
    isBatch.value = true;
  }
};

onMounted(() => {
  getDataList();
});
</script>

<style scoped lang="scss">
.demo-form-inline {
  padding: 0 20px;
}
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.el-form-item__label {
  font-weight: bold;
}

.el-col {
  text-align: left;
}
.el-row {
  width: 90%;
}

.el-form {
  display: inline;
}
</style>
